﻿--[[
   @brief    
   @author   vincentlao
   @version  1.0
   @date     2015-12-03
--]]

-----UI事件监听-------------------------------------------------

friend_search_menu_listener = {

    add_friend_btn_reddot = 0;
    add_qq_friend_menu_reddot = 0;
    add_game_friend_menu_reddot = 0;
    is_im_inited = false;

    show_menu = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        g_friend_search_men:show();
        g_friend_search_men:move(param["x"], param["y"]);

        friend_search_menu_listener.add_friend_btn_reddot = 0;
        friend_list_view.show_friend_search_btn_reddot("false");
        friend_search_menu_listener.save_reddot_status();
    end;

    on_menu_hide = function(uilib, ctrl_id)
        g_friend_search_men:hide();

        friend_search_menu_listener.save_reddot_status();
    end;

    click_on_find_qq_friend = function(data)
        g_friend_search_men:on_click_qq_friend_item(true);
    end;

    click_on_find_new_friend = function(data)
        g_friend_search_men:on_click_search_friend_item();
    end;

    click_on_find_game_friend = function(data)
        g_friend_search_men:on_click_game_friend_item(true);
    end;

    on_click_item = function(uilib, id, item_id, item_index)
        return g_friend_search_men:on_click_item(uilib, id, item_id, item_index);
    end;

    on_im_inited = function(event_id, json_str)
        base_utility.log("[friend_search_menu_listener] im inited");
        friend_search_menu_listener.is_im_inited = true;
        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social:get_history_qq_friend();
        social:get_search_friend_reddot_status();

        friend_search_menu_listener.get_history_game_firend();
    end;

    on_get_search_friend_reddot_status = function(event_id, json_str)
        base_utility.log("[friend_search_menu_listener] on_get_search_friend_reddot_status:"..json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if string.len(param_tbl.status_str) >= 1 then
            param_tbl = base_utility.json_to_table(param_tbl.status_str);
            if param_tbl ~= nil then
                friend_search_menu_listener.add_friend_btn_reddot = param_tbl.btn_reddot;
                friend_search_menu_listener.add_qq_friend_menu_reddot = param_tbl.qq_menu_reddot;
                friend_search_menu_listener.add_game_friend_menu_reddot = param_tbl.game_menu_reddot;
            end
        end
    end;

    get_history_game_firend = function()
        local param_tbl = comm_center.call_service("im_app_service_get_history_game_friend", {});
        if param_tbl.count > 0 then
            friend_list_model.save_history_lol_game_friend_list(param_tbl.game_friends);
        end
    end;

    on_get_game_friend = function(param_tbl)
        if param_tbl.count > 0 then
            friend_list_model.save_new_lol_game_friend_list(param_tbl.game_friends);
        else            
            friend_list_model.save_new_lol_game_friend_list({});
        end
            
        friend_search_menu_listener.is_get_new_game_friend_done = true;
        if friend_search_menu_listener.is_get_tgp_friend_list_done == true then
            friend_search_menu_listener.do_check_game_friend_status();
        end
    end;

    on_get_history_qq_friend_list = function(event_id, json_str)
        --
        local param_tbl = base_utility.json_to_table(json_str);
        local qq_friend_list = nil;
        if param_tbl.count > 0 then
            qq_friend_list = param_tbl.friends;
        end

        local group_info;
        if param_tbl.qq_group_count > 0 then
            group_info = param_tbl.group_info;
        end
        friend_list_model.save_history_qq_friend_list(qq_friend_list, group_info);
    end;


    on_get_qq_friend_list = function(event_id, json_str)
        local param_tbl = base_utility.json_to_table(json_str);

        local new_qq_friend_list = {};
        if param_tbl.count > 0 then
            new_qq_friend_list = param_tbl.friends;
        end

        local new_group_info = {};
        if param_tbl.qq_group_count > 0 then
            new_group_info = param_tbl.group_info;
        end
        friend_list_model.save_new_qq_friend_list(new_qq_friend_list, new_group_info);

        friend_search_menu_listener.is_get_new_qq_friend_done = true;
        if friend_search_menu_listener.is_get_tgp_friend_list_done == true then
            friend_search_menu_listener.do_check_qq_friend_status();
        end
    end;

    --拉取完好友后再判断
    on_get_tgp_friend_list_done = function(event_id, json_str)
        friend_search_menu_listener.is_get_tgp_friend_list_done = true;

        if friend_search_menu_listener.is_get_new_qq_friend_done == true then
            friend_search_menu_listener.do_check_qq_friend_status();
        end

        if friend_search_menu_listener.is_get_new_game_friend_done == true then
            friend_search_menu_listener.do_check_game_friend_status();
        end
    end;

    do_check_game_friend_status = function()
        local new_game_friends = friend_list_model.get_new_lol_game_friend_list();
        local loc_add_game_friend_menu_reddot = 0;

        if new_game_friends ~= nil then
            for index,  one_friend in ipairs(new_game_friends) do
                if one_friend.is_new == 1 then
                    local old_friend = friend_list_model.get_one_history_lol_game_friend(one_friend.friend_id);
                    if old_friend == nil then
                        --在历史记录中不存在   
                        --判断是否好友  
                        if social_utility.is_my_friend(one_friend.friend_id) == false then
                            loc_add_game_friend_menu_reddot = 1;
                            break;
                        end
                    end
                end
            end
        end

        --本次有新增
        if loc_add_game_friend_menu_reddot == 1 then
            -- 菜单显示出来了，就不用设置查找按钮的小红点了
            if g_friend_search_men:is_show() == false then
                friend_search_menu_listener.add_friend_btn_reddot = 1;
            end

            local ret = {is_show = false};
            if game_friend_control ~= nil then
                ret = comm_center.call_service("i_service_add_game_friend_wnd_is_show", {});
            end
            
            --game好友添加界面打开了，就不用显示小红点了
            if ret.is_show ~= true then
                friend_search_menu_listener.add_game_friend_menu_reddot = 1;
            end
        end

        --数据推送
        base_utility.log("[friend_search_menu_listener] do_check_game_friend_status update ui");
        local param = {};
        param.game_friend_list = new_game_friends;
        comm_center.call_service("i_service_add_game_friend_flush_data", param);

        if friend_search_menu_listener.add_friend_btn_reddot == 1 then
            friend_list_view.show_friend_search_btn_reddot("true");
        else
            friend_list_view.show_friend_search_btn_reddot("false");
        end

        if g_friend_search_men:is_show() == true then
            g_friend_search_men:update_reddot_status();
        end

        friend_search_menu_listener.save_reddot_status();
    end;

    do_check_qq_friend_status = function()
        local old_qq_friend_list, old_group_info = friend_list_model.get_history_qq_friend_list();
        local new_qq_friend_list, new_group_info = friend_list_model.get_new_qq_friend_list();
        --检查是否需要显示小红点
        local loc_add_qq_friend_menu_reddot = 0;
        if new_qq_friend_list ~= nil then
            for index, new_qq_friend in ipairs(new_qq_friend_list) do
                if new_qq_friend.is_new == 1 then
                    local old_qq_friend = friend_list_model.get_one_history_qq_friend(new_qq_friend.friend_id);
                    if old_qq_friend == nil then  
                        --在历史记录中不存在   
                        --判断是否好友  
                        if social_utility.is_my_friend(new_qq_friend.friend_id) == false then
                            loc_add_qq_friend_menu_reddot = 1;
                            break;
                        end
                    end
                end
            end
        end

        --本次有新增
        if loc_add_qq_friend_menu_reddot == 1 then
            -- 菜单显示出来了，就不用设置查找按钮的小红点了
            if g_friend_search_men:is_show() == false then
                friend_search_menu_listener.add_friend_btn_reddot = 1;
            end

            local ret = {is_show = false};
            if qq_friend_control ~= nil then
                ret = comm_center.call_service("i_service_add_qq_friend_wnd_is_show", {});
            end
            
            --qq好友添加界面打开了，就不用显示小红点了
            if ret.is_show ~= true then
                friend_search_menu_listener.add_qq_friend_menu_reddot = 1;
            end
        end

        
        --数据推送
        base_utility.log("[friend_search_menu_listener] do_check_qq_friend_status update ui");
        local param = {};
        param.qq_friend_list = new_qq_friend_list;
        param.qq_group_info = new_group_info;
        comm_center.call_service("i_service_add_qq_friend_flush_data", param);

        if friend_search_menu_listener.add_friend_btn_reddot == 1 then
            friend_list_view.show_friend_search_btn_reddot("true");
        else
            friend_list_view.show_friend_search_btn_reddot("false");
        end

        if g_friend_search_men:is_show() == true then
            g_friend_search_men:update_reddot_status();
        end
        friend_search_menu_listener.save_reddot_status();
    end;

    save_reddot_status = function()
        if friend_search_menu_listener.is_im_inited == true then
            local param_tbl = {};
            param_tbl.btn_reddot = friend_search_menu_listener.add_friend_btn_reddot;
            param_tbl.qq_menu_reddot = friend_search_menu_listener.add_qq_friend_menu_reddot;
            param_tbl.game_menu_reddot =friend_search_menu_listener.add_game_friend_menu_reddot;

            local status_str =base_utility.table_to_json(param_tbl);
            local social =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            social:set_search_friend_reddot_status(status_str);
            base_utility.log(string.format("[friend_search_menu_listener] save_reddot_status:%d %d %d", param_tbl.btn_reddot, param_tbl.qq_menu_reddot, param_tbl.game_menu_reddot), true);
        else
            base_utility.log("[friend_search_menu_listener] im is not inited", true);
        end
    end;
};

------------------------------------------------------
friend_search_menu = {
    dwRootId = 0;
    pMenuCtrl = nil;

    LIMIT_CREATE_GROUP_NUMBER = 1;
    LIMIT_TGP_LEVEL = 1;

    switch = {};
};

function friend_search_menu:new()
    local o = {};
    setmetatable(o, self);
    self.__index = self;
    self.is_visible = false;
    return o;
end

function friend_search_menu:is_show()
    return  self.is_visible;
end

function friend_search_menu:hide()
    self.is_visible = false;
    self:remove_ctrl();
end

function friend_search_menu:remove_ctrl()
    layout_mgr.delay_remove_ctrl(self.dwRootId);
    self.dwRootId = 0;
    self.pMenuCtrl = nil;
end

function friend_search_menu:show()
    --乒乓效果
    if self.pMenuCtrl ~= nil and self.pMenuCtrl:IsVisible() == TRUE then
        self.pMenuCtrl:Show(FALSE);
        return;
    end

    if self:create_menu_ctrl()  then
        self:create_menu_item();
        self.is_visible = true;
    else
        base_utility.log("[friend_search_menu:show] create menu ctrl error", true);
    end
end

function friend_search_menu:move(x, y) -- x, y是“添加”按钮的右下角相对屏幕坐标
    if self.pMenuCtrl then
        local mainWnd = ui_utility.get_ctrl_ptr_by_cid(CID_SOCIAL_PANEL_DLG);
        local mainRc = mainWnd:GetRectEx(TRUE, TRUE);

        local rc = self.pMenuCtrl:GetRectEx(TRUE,TRUE);
        local w = rc.right - rc.left;
        local h = rc.bottom - rc.top;

        rc.left = x - mainRc.left;
        rc.right = rc.left + w;
        rc.top = y - mainRc.top - h;
        rc.bottom = rc.top + h;

        self.pMenuCtrl:MoveEx(rc,TRUE);
        self.pMenuCtrl:Show(TRUE);
    end
end

function friend_search_menu.is_wegamehk()
    return base_utility.is_type_of_oversea_ver();
end

function friend_search_menu:create_menu_item()
    ierd_tgp.tpf_ui.Require("social\\common_menu_item\\common_menu_item.lua")
    self.pMenuCtrl:RemoveAll();

    -- 菜单上部分空白
    local ui_str, str_hodler = U8toCUIStr("social\\common_menu_item_blank.tpf");
    self.pMenuCtrl:AppendItem(ui_str);

    self.switch = {};

    local s_add_friend = LoadStr("SOCIAL_FRIEDN_LIST_SEARCH_FRIEND");
    local menu_item = common_menu_item:new(self.pMenuCtrl, common_menu_item.WITH_NONE, s_add_friend);
    self.switch[menu_item:get_item_id()] = self.on_click_search_friend_item;

    local menu_item = common_menu_item:new(self.pMenuCtrl, common_menu_item.WITH_NONE, LoadStr("SOCIAL_FRIEDN_LIST_SEARCH_GROUP"));
    self.switch[menu_item:get_item_id()] = self.on_click_search_group_item;

    if not base_utility.is_type_of_oversea_ver() then
        self.game_menu = common_menu_item:new(self.pMenuCtrl, common_menu_item.WITH_NONE, LoadStr("SOCIAL_FRIEDN_LIST_SEARCH_ADDGAME"));
        self.switch[self.game_menu:get_item_id()] = self.on_click_game_friend_item;
        if friend_search_menu_listener.add_game_friend_menu_reddot == 1 then
            self.game_menu:show_red_dot(true);
        end
    end


    -- 菜单下部分空白
    ui_str, str_hodler = U8toCUIStr("social\\common_menu_item_blank.tpf");
    self.pMenuCtrl:AppendItem(ui_str);
end

function friend_search_menu:create_menu_ctrl()
    if self.pMenuCtrl ~= nil then
        return true;
    end

    local socail_wnd_id = ui_utility.get_ctrl_id_by_cid(CID_SOCIAL_PANEL_DLG);
    self.dwRootId = layout_mgr.restore_sub_layout(socail_wnd_id, "social\\friend_search_menu.tpf");
    self.pMenuCtrl = ui_utility.get_ctrl_ptr_by_id(self.dwRootId);
    if self.pMenuCtrl ~= nil then
        return true;
    else
        return false;
    end
end

function friend_search_menu:on_click_item(uilib, id, item_id, item_index)
    local fun = self.switch[item_id];
    if fun ~= nil then
        return fun();
    else
        return true;
    end
end

function friend_search_menu:on_click_search_friend_item()
    local social =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    local webkit_switch = social:get_webkit_switch();

    local param = {};
    if webkit_switch == 1 then
        param = {url = "/sociality/search.html?tab=1"};
    else
        param = {url = "/sociality-1.1/search.html?tab=1"};
    end
    event_center.send_event(group_event.SHOW_GROUP_SEARCH_DIALOG,base_utility.table_to_json(param));

    comm_center.call_service('i_service_tqos_friend_search_menu_nick_click', {});

    return true;
end

function friend_search_menu:on_click_game_friend_item(is_come_from_guide)
    friend_search_menu_listener.add_game_friend_menu_reddot = 0;
    friend_search_menu_listener.save_reddot_status();
    local param = {};
    event_center.send_event(group_event.SHOW_GAME_FRIEND_DIALOG,base_utility.table_to_json(param));

    -- 如果是来自没有好友时的引导，要考虑当前的状态
    -- 如果好友搜索、游戏搜索小红点没有了，外面的菜单入口红点需要取消掉
    if is_come_from_guide == true then
        if friend_search_menu_listener.add_qq_friend_menu_reddot == 0 and 
           friend_search_menu_listener.add_friend_btn_reddot == 1 then

            friend_search_menu_listener.add_friend_btn_reddot = 0;
            friend_list_view.show_friend_search_btn_reddot("false");
       end
        friend_search_menu_listener.save_reddot_status();
    end
    
    comm_center.call_service('i_service_tqos_friend_search_menu_game_click', {});
    return true;
end

function friend_search_menu:on_click_qq_friend_item(is_come_from_guide)
    friend_search_menu_listener.add_qq_friend_menu_reddot = 0;
    friend_search_menu_listener.save_reddot_status();
    local param = {};
    event_center.send_event(group_event.SHOW_QQ_FRIEND_DIALOG,base_utility.table_to_json(param));

    -- 如果是来自没有好友时的引导，要考虑当前的状态
    -- 如果好友搜索、游戏搜索小红点没有了，外面的菜单入口红点需要取消掉
    if is_come_from_guide == true then
        if friend_search_menu_listener.add_game_friend_menu_reddot == 0 and 
           friend_search_menu_listener.add_friend_btn_reddot == 1 then
           
            friend_search_menu_listener.add_friend_btn_reddot = 0;
            friend_list_view.show_friend_search_btn_reddot("false");
       end
        friend_search_menu_listener.save_reddot_status();
    end

    comm_center.call_service('i_service_tqos_friend_search_menu_qq_click', {});
    return true;
end

function friend_search_menu:update_reddot_status()
    if friend_search_menu_listener.add_game_friend_menu_reddot == 1 and self.game_menu ~= nil then
        self.game_menu:show_red_dot(true);
    end
    if friend_search_menu_listener.add_qq_friend_menu_reddot == 1 and self.qq_menu ~= nil  then
        self.qq_menu:show_red_dot(true);
    end
end


function friend_search_menu:on_click_search_group_item()
    local param = {};
    local social =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    local webkit_switch = social:get_webkit_switch();
    if webkit_switch == 1 then
        param = {url = "/sociality/search.html?tab=0"};
    else
        param = {url = "/sociality-1.1/search.html?tab=0"};
    end
    event_center.send_event(group_event.SHOW_GROUP_SEARCH_DIALOG,base_utility.table_to_json(param));
    comm_center.call_service('i_service_tqos_group_search_menu_find_click', {});
    return true;
end

g_friend_search_men = friend_search_menu:new();